from __future__ import annotations

import datetime

import arcade


class DigitalClockView(arcade.View):
    def __init__(self):
        super().__init__()
        
        # 初始化背景颜色
        self.background_color = arcade.color.BLACK
        
        # 时间显示参数
        self.time_text = datetime.datetime.now().strftime("%H:%M:%S")
        self.text_color = arcade.color.LIME_GREEN
        self.font_size = 64
        
        # 上次更新时间
        self.last_update = datetime.datetime.now()
        
        # 设置窗口标题
        self.window.set_caption("24小时制数字时钟")

    def on_show_view(self):
        """ 当视图显示时调用 """
        arcade.set_background_color(self.background_color)

    def on_draw(self):
        """ 绘制界面 """
        self.clear()
        
        # 获取窗口尺寸
        width, height = self.window.width, self.window.height
        
        # 绘制时间文本
        arcade.draw_text(
            self.time_text,
            width // 2,
            height // 2,
            self.text_color,
            self.font_size,
            anchor_x="center",
            anchor_y="center",
            font_name=("Arial", "Microsoft YaHei")
        )

    def on_update(self, delta_time: float):
        """ 更新逻辑（每秒60次） """
        now = datetime.datetime.now()
        
        # 每秒更新一次时间显示
        if (now - self.last_update).total_seconds() >= 1:
            self.time_text = now.strftime("%H:%M:%S")
            self.last_update = now

def main():
    # 创建窗口
    window = arcade.Window(
        width=400,
        height=200,
        resizable=True
    )
    
    # 设置初始视图
    window.show_view(DigitalClockView())
    
    # 运行程序
    arcade.run()

if __name__ == "__main__":
    main()